package dk.eaaa.mobil2012.indkoebsappnyere;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class AddProduct extends Fragment {

	private ListView listView;
	private MainActivity activity;
	private ArrayList<View> selectedViews;
	private Button addButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.addproducts, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		activity = (MainActivity) getActivity();

		ArrayList<Product> products = activity.getProducts();
		selectedViews = new ArrayList<View>();
		listView = (ListView) getActivity().findViewById(R.id.addproducts);
		listView.setAdapter(new ArrayAdapter<Product>(getActivity(),
				android.R.layout.simple_list_item_1, products));
		addButton = (Button) activity.findViewById(R.id.addToShoppinglist);
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				Product tempProduct = (Product) adapter
						.getItemAtPosition(position);
				if (selectedViews.contains(view)) {
					view.setBackgroundColor(Color.WHITE);
					selectedViews.remove(view);
					activity.removeFromSelectedProducts(tempProduct);
				} else {
					view.setBackgroundColor(Color.CYAN);
					selectedViews.add(view);
					activity.addToSelectedProducts(tempProduct);
				}
				if (selectedViews.size() > 0) {
					addButton.setEnabled(true);
				} else {
					addButton.setEnabled(false);
				}
			}
		});
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> adapter, View view,
					int position, long id) {
				Product tempProduct = (Product) adapter
						.getItemAtPosition(position);
				activity.setCurrentProduct(tempProduct);
				AlertDialog.Builder builder = new AlertDialog.Builder(
						getActivity());
				builder.setMessage(tempProduct.getName());
				builder.setPositiveButton("Details",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								FragmentManager manager = getFragmentManager();
								FragmentTransaction transaction = manager
										.beginTransaction();
								Details details = new Details();
								transaction.add(R.id.frame, details, "details");
								transaction.attach(details);
								transaction.detach(AddProduct.this);
								transaction.addToBackStack(null);
								transaction.commit();
								activity.getActionBar().hide();
							}
						});
				builder.setNegativeButton("Edit",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								FragmentManager manager = getFragmentManager();
								FragmentTransaction transaction = manager
										.beginTransaction();
								Edit edit = new Edit();
								transaction.add(R.id.frame, edit, "edit");
								transaction.attach(edit);
								transaction.detach(AddProduct.this);
								transaction.addToBackStack(null);
								transaction.commit();
								activity.getActionBar().hide();

							}
						});
				builder.setNeutralButton("Map", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						AlertDialog.Builder gpsbuilder = new AlertDialog.Builder(getActivity());
						gpsbuilder.setMessage(activity.getCurrentProduct().getName());
						gpsbuilder.setPositiveButton("Aarhus N", new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:56.178283,10.200471?z=16"));
								startActivity(intent);
							}
						});
						gpsbuilder.setNeutralButton("Aarhus V", new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:56.171104,10.189356?z=16"));
								startActivity(intent);
							}
						});
						gpsbuilder.setNegativeButton("Viby J", new DialogInterface.OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:56.129929,10.160603?z=16"));
								startActivity(intent);
							}
						});
						AlertDialog gpsAlert = gpsbuilder.create();
						gpsAlert.show();
					}
				});
				AlertDialog alert = builder.create();
				alert.show();
				return true;
			}
		});
	}
}
